﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCM.UI.Utils;
using Baosight.Platform.Core;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCM.UI.ChildWindows;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMA0 : EFViewCmes
    {
        private MonthBlock monthblock = new MonthBlock();//构造前台下拉列表
        public ZMCMA0()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(monthblock.GetMonth(true));
            base.Initialize(initData);

        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 物资调价管理新增操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "物资调价管理-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 物资调价管理修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            bool canDelete = true;
            String msg = "";
            if (_list.Count <= 0)
            {
                msg = "请选择一条记录进行修改！";
                canDelete = false;
            }
            else
            {
                if (this.grid.GetCheckedEIRows()[0]["fdChangeStatus"].ToString().Equals("生效"))
                {
                    msg = "选择的记录已经生效，不允许修改！";
                    canDelete = false;
                }
            }
            if (!canDelete)
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }

            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "物资调价管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 物资调价管理删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIRow row = new EIRow();
            bool canDelete = true;
            String msg = "";
            if (_list.Count <= 0)
            {
                msg = "请选择要删除的记录(一条或多条)！";
                canDelete = false;
            }
            else
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    if (this.grid.GetCheckedEIRows()[i]["fdChangeStatus"].ToString().Equals("生效"))
                    {
                        msg = "选择的记录已经生效或包含有已经生效的记录，不允许删除！";
                        canDelete = false;
                        break;
                    }
                }
            }
            if (canDelete)
            {

                base.OnDeleteButtonClick("ZMCMA0", "delete", sender, e);
            }
            else
            {
                this.OpenEFWindowAlert(msg, true);
                return;
            }
        }

        /// <summary>
        /// 生效操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnEffectButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            (sender as Button).IsEnabled = false;
            EIInfo eiInfo = new EIInfo();
            EIRow row = new EIRow();

            if (_list.Count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行生效！", true);
                return;
            }
            else if (_list.Count > 1)
            {
                this.OpenEFWindowAlert("一次只能生效一条记录！", true);
                return;
            }
            if (this.grid.GetCheckedEIRows()[0]["fdChangeStatus"].ToString().Equals("草拟"))
            {

                base.OnModifyButtonClick("ZMCMA0", "confirm", "生效", sender, e);
            }
            else
            {
                base.OpenEFWindowAlert("该记录已经生效，不能重复生效！", true);
            }

        }

        /// <summary>
        /// 导出操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnExportButtonClick(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
            }
            base.TempButtonOpen(true);//按钮生效
        }

        /// <summary>
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            ChangePriceHis zmcma0 = new ChangePriceHis(dialogType);//构造一个新的修改页面
            zmcma0.EIDataContext = new Platform.Core.UI.EIDataContext();
            zmcma0.EIDataContext.Update(eIInfo);
            if ("insert".Equals(dialogType))
            {
                zmcma0.fdChangeDate.Value = DateTime.Now.ToString("yyyy-MM-dd");//得到当前日期
                zmcma0.fdChanger.Value = this.EIDataContext.GetAttribute("userId").ToString();//得到当前登录用户Id
                zmcma0.fdChangeDepartment.Value = this.EIDataContext.GetAttribute("orgName").ToString();//得到当前登录用户所属单位
                zmcma0.fdChangeMonth.Value = Convert.ToInt32(DateTime.Now.ToString("MM"));//得到当前月份
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = zmcma0;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();
        }

    }

}
